package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/25 9:18
 */
public class C8 {

    public static int myAtoi(String s) {
        if ((s = s.trim()).length() == 0) return 0;
        char[] str = s.toCharArray();
        int i = 0, ans = 0;
        boolean pos = true;
        if (str[0] == '+' || str[0] == '-') pos = str[i++] == '+';
        for (; i < str.length; i++) {
            if (str[i] > '9' || str[i] < '0') break;
            if (ans > Integer.MAX_VALUE / 10 || (ans == Integer.MAX_VALUE / 10 && str[i] > '7')) return pos ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            ans = ans * 10 + (str[i] - '0');
        }
        return pos ? ans : ~ans + 1;
    }


    public static void main(String[] args) {
//        String s = "  ";
//        String s = String.valueOf(Integer.MAX_VALUE);
        String s = String.valueOf(Integer.MIN_VALUE);
        System.out.println(myAtoi(s));
    }

}
